home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_02_11 / 2n11050b < prev    next >
Text File  |  1991-04-16  |  5KB  |  238 lines

  1. /**********************************************/
  2. /* Listing 2.                                 */
  3. /*                                            */
  4. /* Module to write text on any bit boundry    */
  5. /* on EGA/VGA graphics screen                 */
  6. /* For Borland C++ Compiler.                  */
  7. /*                                            */
  8. /* (c)copyright 1991, Michael Chapin          */
  9. /**********************************************/
  10.  
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include <dos.h>
  14. #include <string.h>
  15. #include <bfont.h>
  16.  
  17.  
  18. BFont BF[] =
  19.   {{7, 8, 8, NULL},
  20.    {7, 8, 14, NULL},
  21.    {7, 8, 16, NULL}};
  22.  
  23.  
  24. BOOL BFontsLoaded;
  25. unsigned CurrBFFont;
  26.  
  27. unsigned VideoSeg = 0xA000;/* current video segment */
  28. unsigned ScnWidth;         /* screen width in bytes */
  29. unsigned ScrMode;          /* current grapics mode  */
  30.  
  31.  
  32. void GetBFFontAdr(unsigned Which)
  33. {
  34.   struct REGPACK Regs;
  35.   unsigned num;
  36.  
  37.   switch(Which){
  38.     case 0 : num = 3; break;
  39.     case 1 : num = 2; break;
  40.     case 2 : num = 6;
  41.   }
  42.   Regs.r_ax = 0x1130;
  43.   Regs.r_bx = num << 8;
  44.   intr(0x10, &Regs);
  45.   BF[Which].FontPtr = MK_FP(Regs.r_es, Regs.r_bp);
  46. }
  47.  
  48. unsigned getvideomode()
  49. {
  50.   struct REGPACK Regs;
  51.  
  52.   Regs.r_ax = 0x0F00;
  53.   intr(0x10, &Regs);
  54.   return Regs.r_ax & 0xFF;
  55. }
  56.  
  57. void SetBFonts()
  58. {
  59.   unsigned Mode;
  60.  
  61.   Mode = getvideomode();
  62.   if(!(Mode >= 13 && Mode <= 18))
  63.     exit(1);
  64.   VideoSeg = 0xA000;
  65.   if(Mode == 13)
  66.     ScnWidth = 40;
  67.   else
  68.     ScnWidth = 80;
  69.   GetBFFontAdr(0);
  70.   GetBFFontAdr(1);
  71.   if(Mode > 16)
  72.     GetBFFontAdr(2);
  73.   BFontsLoaded = TRUE;
  74.   ScrMode = Mode;
  75. }/* SetBFonts */
  76.  
  77.  
  78. /*** Low level routine to write to screen ***/
  79.  
  80. unsigned shift;
  81.  
  82. unsigned GetOffset(unsigned x, unsigned y)
  83. {
  84.   unsigned ofs, l;
  85.  
  86.   ofs = x >> 3;            /* byte offset for col */
  87.   if(ScrMode >= 13)  /* add offset for row  */
  88.     ofs += y * ScnWidth;
  89.   shift = x % 8;
  90.   return ofs;
  91. }/* GetOffset */
  92.  
  93. void SetWrt2(void)
  94. {
  95.   outportb(0x3CE, 5);  /*index 5 to address register*/
  96.   outportb(0x3CF, 2);  /*send out wrt mode num */
  97. }/* SetWrt2 */
  98.  
  99. void SetWrt0(void)
  100. {
  101.   outportb(0x3CE, 5);  /*index 5 to address register*/
  102.   outportb(0x3CF, 0);  /*send out wrt mode num */
  103. }/* SetWrt0 */
  104.  
  105. void print_char(unsigned x, unsigned y,
  106.                 unsigned color, unsigned height,
  107.                 unsigned char far *pattern)
  108. {
  109.   unsigned char far *scnptr, far *ts;
  110.   unsigned c;
  111.   unsigned linebyte;
  112.   unsigned char scrbyte;
  113.   unsigned char lbyte, rbyte;
  114.  
  115.   scnptr = MK_FP(VideoSeg, GetOffset(x, y));
  116.   /* set ega/vga write mode 2 */
  117.   SetWrt2();
  118.  
  119.   /* loop through height of character */
  120.   for(c = 1; c <= height; c++)
  121.   {
  122.     ts = scnptr;
  123.     linebyte = *pattern << 8;
  124.     if(shift)
  125.       linebyte >>= shift;
  126.  
  127.     lbyte = linebyte >> 8;
  128.     rbyte = linebyte;
  129.  
  130.     /* address the bit map register */
  131.     outportb(0x3CE, 8);
  132.     /* send upper byte */
  133.     outportb(0x3CF, lbyte);
  134.     /* latch the screen byte */
  135.     scrbyte = *ts;
  136.     /* write masked byte to the screen */
  137.     *ts = color;
  138.     /* do the right byte if needed */
  139.     if(shift)
  140.     {
  141.       ++ts;
  142.       outportb(0x3CE, 8);
  143.       outportb(0x3CF, rbyte);
  144.       scrbyte = *ts;
  145.       *ts = color;
  146.     }
  147.  
  148.     scnptr += ScnWidth;
  149.     ++pattern;
  150.   }
  151.  
  152.   SetWrt0();
  153. }/* print_char */
  154.  
  155. void EGABlock(unsigned x, unsigned y,
  156.               unsigned width, unsigned height,
  157.               unsigned color)
  158. {
  159.   unsigned char ml, mr, scrbyte;
  160.   unsigned bytewidth, i;
  161.   unsigned char far *scnptr, far *ts;
  162.  
  163.   ml = 0xFF >> (x % 8);
  164.   mr = 0xFF << (8 - ((x + width) % 8));
  165.   bytewidth = (width >> 3) + 1;
  166.  
  167.   /* This makes vertical lines possible */
  168.   if(bytewidth == 1)
  169.     ml = ml & mr;
  170.   scnptr = MK_FP(VideoSeg, GetOffset(x, y));
  171.   SetWrt2();
  172.  
  173.   while(height)
  174.   {
  175.     ts = scnptr;
  176.     outportb(0x3CE, 8);
  177.     outportb(0x3CF, ml);
  178.     scrbyte = *ts;
  179.     *ts = color;
  180.     ++ts;
  181.     if(bytewidth > 1)
  182.     {
  183.       if(bytewidth > 2)
  184.       {
  185.         for(i = 0; i < bytewidth - 1; i++)
  186.         {
  187.           outportb(0x3CE, 8);
  188.           outportb(0x3CF, 0xFF);
  189.           scrbyte = *ts;
  190.           *ts = color;
  191.           ++ts;
  192.         }
  193.       }
  194.       outportb(0x3CE, 8);
  195.       outportb(0x3CF, mr);
  196.       scrbyte = *ts;
  197.       *ts = color;
  198.     }
  199.     --height;
  200.     scnptr += ScnWidth;
  201.   }
  202.   SetWrt0();
  203. }
  204.  
  205. void PrintBF(unsigned x, unsigned y, char *Msg)
  206. {
  207.   unsigned StrLen, i;
  208.   unsigned char far *PatPtr;
  209.  
  210.   if(!BFontsLoaded)
  211.     return;
  212.   StrLen = strlen(Msg);
  213.   for(i = 0; i < StrLen; i++)
  214.   {
  215.     PatPtr = BF[CurrBFFont].FontPtr;
  216.     PatPtr += Msg[i] * BF[CurrBFFont].Height;
  217.     print_char(x, y, BF[CurrBFFont].Color,
  218.                BF[CurrBFFont].Height, PatPtr);
  219.     x += BF[CurrBFFont].Width;
  220.   }
  221. }/* PrintBF */
  222.  
  223. void SetBFFont(unsigned Which)
  224. {
  225.   if(BF[Which].FontPtr == NULL)
  226.     CurrBFFont = 0;
  227.   else
  228.     CurrBFFont = Which;
  229. }/* SetBFFont */
  230.  
  231. void SetBFColor(unsigned Which, unsigned Color)
  232. {
  233.   /* make sure color is within range */
  234.   Color = Color & 0xF;
  235.   BF[Which].Color = Color;
  236. }/* SetBFColor */
  237.  
  238.